#!/bin/bash
data_path={{ fastdfs_data_path }}
base_path={{ dest_dir }}
install_path={{ fastdfs_dir }}
tracker_addr={{ groups['one_install'][0] }}
tracker_port={{ tacker_port }}
storage_addr={{ groups['one_install'][0] }}
storage_port={{ storage_port }}
ngx_port={{ fasfng_port }}
fastnginx_path={{ fastng_dir }}

systemctl stop firewalld.service
 
yum install -y unzip zip zlib zlib-devel pcre-devel net-tools telnet gcc gcc=c++ libstdc++  make cmake autoconf jemalloc ncurses ncurses-devel

mkdir -p ${data_path}/{storage,tracker}
mkdir -p ${data_path}/storage/{data,logs}

##add env

cat > /etc/profile.d/fastdfs.sh <<-EOF
export FASTDFS_HOME=${install_path}
export PATH=\${PATH}:\${FASTDFS_HOME}/bin
EOF

cd $base_path
cd libfastcommon-1* && ./make.sh && ./make.sh install

cd $base_path
cd FastDFS* && sed -i 's@^TARGET_PREFIX=.*@TARGET_PREFIX='${install_path}'@' make.sh && \
            sed -i 's@^TARGET_CONF_PATH=.*@TARGET_CONF_PATH='${install_path}'/fdfs@' make.sh && \
            ./make.sh && ./make.sh install 
#修改storage.conf文件配置
sed -i 's@bind_addr=.*@bind_addr=0.0.0.0@g' ${install_path}/fdfs/storage.conf
sed -i 's@base_path=.*@base_path='${data_path}'/storage@g' ${install_path}/fdfs/storage.conf
sed -i 's@store_path0=.*@store_path0='${data_path}'/storage@g' ${install_path}/fdfs/storage.conf
sed -i 's@^tracker_server=.*@tracker_server='${tracker_addr}':'${tracker_port}'@g' ${install_path}/fdfs/storage.conf

#修改tracker
sed -i 's@bind_addr=.*@bind_addr=0.0.0.0@g' ${install_path}/fdfs/tracker.conf
sed -i 's@base_path=.*@base_path='${data_path}'/tracker@g' ${install_path}/fdfs/tracker.conf

#修改client.conf文件 
sed -i 's@^base_path=.*@base_path='${data_path}'/storage@g' ${install_path}/fdfs/client.conf
sed -i 's@tracker_server=.*@tracker_server='${tracker_addr}':'${tracker_port}'@g' ${install_path}/fdfs/client.conf

ln -s ${install_path}/lib/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s ${install_path}/lib/libfastcommon.so /usr/lib/libfastcommon.so
ln -s ${install_path}/lib/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s ${install_path}/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
ln -s ${install_path}/lib/libfdfsclient.so /usr/lib64/libfdfsclient.so
#start
cd ${base_path} && cd FastDFS* && \cp init.d/* /etc/init.d/
sed -i 's@/usr/local@'${install_path}'@g' /etc/init.d/fdfs_storaged
sed -i 's@^CONF=/etc@CONF='${install_path}'@g' /etc/init.d/fdfs_storaged
sed -i 's@/usr/local@'${install_path}'@g' /etc/init.d/fdfs_trackerd
sed -i 's@^CONF=/etc@CONF='${install_path}'@g' /etc/init.d/fdfs_trackerd
chmod +x /etc/init.d/fdfs_trackerd
chmod +x /etc/init.d/fdfs_storaged
systemctl daemon-reload
/etc/init.d/fdfs_trackerd restart
sleep 1
/etc/init.d/fdfs_storaged restart

#install nginx
cd ${base_path}
cat > fastdfs-nginx-module/src/config <<-EOF
ngx_addon_name=ngx_http_fastdfs_module
HTTP_MODULES="\$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="\$NGX_ADDON_SRCS \$ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="\$CORE_INCS ${install_path}/include/fastdfs ${install_path}/include/fastcommon/"
CORE_LIBS="\$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
CFLAGS="\$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"${install_path}/fdfs/mod_fastdfs.conf\"'"
EOF

cd nginx*
./configure --prefix=${fastnginx_path} --sbin-path=${fastnginx_path}/sbin/nginx --conf-path=${fastnginx_path}/nginx.conf \
            --pid-path=${fastnginx_path}/logs/nginx.pid --with-http_realip_module --add-module=../fastdfs-nginx-module/src
make -j 2 && make install

#修改mod_fastfdfs.conf配置文件
cd ${base_path}
\cp fastdfs-nginx-module/src/mod_fastdfs.conf ${install_path}/fdfs/

sed -i 's@base_path=.*@base_path='${data_path}'/storage@g' ${install_path}/fdfs/mod_fastdfs.conf
sed -i 's@tracker_server=.*@tracker_server='${tracker_addr}':'${tracker_port}'@g' ${install_path}/fdfs/mod_fastdfs.conf
sed -i 's@url_have_group_name.*@url_have_group_name=true@g' ${install_path}/fdfs/mod_fastdfs.conf
sed -i 's@store_path0=.*@store_path0='${data_path}'/storage@g' ${install_path}/fdfs/mod_fastdfs.conf
sed -i 's@group_count = 0@group_count = 1@g' ${install_path}/fdfs/mod_fastdfs.conf

#添加组信息
cat >> ${install_path}/fdfs/mod_fastdfs.conf <<-EOF
[group1]
group_name=group1
storage_server_port=${storage_port}
store_path_count=1
store_path0=${data_path}/storage
EOF

cat > ${fastnginx_path}/nginx.conf <<-'EOF'
worker_processes 2;
error_log_samu;
pid_samu;
events {
    worker_connections 2024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    log_format main '[$time_local] $remote_addr "$http_x_forwarded_for" $remote_port $remote_user '
         '$host $upstream_addr $upstream_status $upstream_response_time $upstream_cache_status '
         '"$request" $status $request_time $body_bytes_sent "$http_referer" "$http_user_agent" ';
     access_log_samu;
server {
    listen_samu;
    server_name _;
    location ~/group[0-9]/ {
            ngx_fastdfs_module;
        }
    }
}
EOF

sed -i 's@error_log_samu@error_log '${fastnginx_path}'/logs/error_log.log@g' ${fastnginx_path}/nginx.conf 
sed -i 's@access_log_samu@access_log '${fastnginx_path}'/logs/access_log.log@g' ${fastnginx_path}/nginx.conf 
sed -i 's@pid_samu@pid '${fastnginx_path}'/logs/nginx.pid@g' ${fastnginx_path}/nginx.conf 
sed -i 's@listen_samu@listen '${ngx_port}'@g' ${fastnginx_path}/nginx.conf

#启动nginx
ps -ef|grep -v grep|grep "${fastnginx_path}/sbin/nginx" && ${fastnginx_path}/sbin/nginx -s stop || ${fastnginx_path}/sbin/nginx
#添加开机启动
chmod +x /etc/rc.d/rc.local
grep "${fastnginx_path}/sbin/nginx" /etc/rc.local || echo "${fastnginx_path}/sbin/nginx -c ${fastnginx_path}/nginx.conf" >> /etc/rc.local
grep "/etc/init.d/fdfs_trackerd" /etc/rc.local || echo "/etc/init.d/fdfs_trackerd restart" >> /etc/rc.local
grep "/etc/init.d/fdfs_storaged" /etc/rc.local || echo "/etc/init.d/fdfs_storaged restart" >> /etc/rc.local

